package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.redis.domian.StrategyStatisVO;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.luowowo.search.qo.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.EsStatisVo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVoRedisService StrategyStatisVoRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public Object index(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){

        //国内外排行榜查询
        //abroadCds    unabroadCds  所有的list
        List<StrategyStatisVO> vos = StrategyStatisVoRedisService.listIsAbroadCommendVo();

        //进行拆分
        List<StrategyStatisVO>  abroadCds = new ArrayList<>();
        List<StrategyStatisVO>  unabroadCds = new ArrayList<>();

        for (StrategyStatisVO vo : vos) {
            if(vo.isIsabroad()){
                if (abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else{
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }

        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);
        //攻略推荐封面 //commend

        model.addAttribute("commends",strategyCommendService.getTop5());
        //热门推荐排行榜前10     //hotCds

        model.addAttribute("hotCds",StrategyStatisVoRedisService.listHotVoTop10());

        //themeCds主题推荐
        //优化: 1.城市重复,2.没有排序,3.前10个
        List<Map<String,Object>> themeCds =  strategySearchService.queryThemeTop10();
        model.addAttribute("themeCds",themeCds);

        //themes  主题推荐  家庭(4)
        List<EsStatisVo> themes = strategySearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);

        //chinas 国内推荐 广东(4)

        List<EsStatisVo> chinas = strategySearchService.queryConditioncChinasGroup();
        model.addAttribute("chinas",chinas);

        //abroads 国外推荐  韩国(4)

        List<EsStatisVo> abroads = strategySearchService.queryConditioncAbroadsGroup();
        model.addAttribute("abroads",abroads);

        return "/strategy/index";
    }


    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo")StrategySearchQuery qo){
        Page page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "/strategy/searchPageTpl";
    }

    /**
     * 攻略明显全部
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //吐司
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);

        //tags
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo
        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "/strategy/list";
    }

    @RequestMapping("/detail")
    public Object detail(Model model, Long id,@UserParam UserInfo userInfo){
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail",detail);


        //查看一次就添加一次阅读数
        StrategyStatisVoRedisService.viewnumIncrease(id,1);
        //查询
        StrategyStatisVO vo = StrategyStatisVoRedisService.getStrategyStatisVo(id);
        model.addAttribute("vo",vo);

        //
        if(userInfo!=null){
            Boolean rest = StrategyStatisVoRedisService.getbyUserIdAndSid(id,userInfo.getId());
            System.err.println(rest);
            model.addAttribute("isFavor",rest);
        }

        //热门攻略排行加分 +1
        StrategyStatisVoRedisService.addHotScore(id,1);

        return "/strategy/detail";
    }

    //添加攻略评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object detail(StrategyComment comment, @UserParam UserInfo userInfo){

        if(userInfo==null){
            throw new LogicException("请登录后再评论");
        }

        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setCity(userInfo.getCity());
        comment.setUsername(userInfo.getNickname());
        comment.setCreateTime(new Date());
        strategyCommentService.save(comment);

        //评论完将评论数+1
        int reply =  StrategyStatisVoRedisService.replynumIncrease(comment.getDetailId(),1);
        //热门攻略排行加分 +1
        StrategyStatisVoRedisService.addHotScore(comment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(reply);



    }

   //攻略评论查询列表
    @RequestMapping("/comment")
    public Object comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        model.addAttribute("c",page.getContent());
        return "/strategy/commentTpl";
    }

    //攻略评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,@UserParam UserInfo userInfo){
        //点赞或者取消点赞
        strategyCommentService.commentThumbUp(toid,userInfo.getId());
        return AjaxResult.SUCCESS;
    }

    //攻略收藏/取消收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo==null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        //如果是true 表示收藏
        Boolean ret = StrategyStatisVoRedisService.favor(sid,userInfo.getId());
        //如果ret返回true才是收藏 +1
        //否则 -1分
        StrategyStatisVoRedisService.addIsAbroadCommend(sid,ret?1:-1);
        //如果是false 表示取消收藏
        return new AjaxResult(ret,null).addData(StrategyStatisVoRedisService.getStrategyStatisVo(sid));
    }

    //点赞(顶)
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo==null){
            AjaxResult result = new AjaxResult(false, "请先登录后点赞(顶)");
            result.setCode(102);
            return result;
        }

        //点赞
        Boolean ret = StrategyStatisVoRedisService.thumbup(sid,userInfo.getId());

            //如果ret返回true才是第一次点赞 +1
            //否则 -1分
            //国内外攻略推荐点赞
            StrategyStatisVoRedisService.addIsAbroadCommend(sid,ret?1:-1);


        return new AjaxResult(ret,null).addData(StrategyStatisVoRedisService.getStrategyStatisVo(sid));
    }
}
